<template>
    <div>
      <div style="margin: 10px 0">
        <div style="margin: 10px 0">
          <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-user" v-model="name"></el-input>
          <el-button class="mr-l-5" type="primary" @click="load">查询</el-button>
          <el-button class="mr-l-5" type="warning" @click="reset">重置</el-button>
        </div>
        <el-upload action="http://localhost:8000/bookmall/file/upload"  :show-file-list=false :on-success="handleFileUploadSuccess" style="display: inline-block;" class="mr-l-5">
          <el-button type="primary">上传文件 <i class="el-icon-upload"></i></el-button>
        </el-upload>
        <el-button type="danger" @click="delBath" class="mr-l-5">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </div>
      <el-table :data="tableData"  stripe border header-cell-class-name="headerBg"
                @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="name" label="文件名称" ></el-table-column>
        <el-table-column prop="type" label="文件类型" ></el-table-column>
        <el-table-column prop="size" label="文件大小" ></el-table-column>
        <el-table-column label="预览">
          <template slot-scope="scope">
            <el-button type="primary" @click="preview(scope.row.url)">预览</el-button>
          </template>
        </el-table-column>
        <el-table-column  label="下载" width="120">
          <template slot-scope="scop">
            <el-button type="primary" @click="downLoad(scop.row.url)">下载</el-button>
          </template>
        </el-table-column>
        <el-table-column label="启用"  >
          <template slot-scope="scop">
            <el-switch v-model="scop.row.enable" @change="changEnable(scop.row)" active-color="#13ce66"
                       inactive-color="#ff4949"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-popconfirm
                confirm-button-text='好的'
                cancel-button-text='不用了'
                icon="el-icon-info"
                icon-color="red"
                title="这是一段内容确定删除吗？"
                @confirm="handleDelete( scope.row.id)"
            >
              <el-button
                  size="mini"
                  type="danger" slot="reference" class="mr-l-5">删除<i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div  class="block" style="padding: 10px 0">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalNum">
        </el-pagination>
      </div>
    </div>
</template>

<script>
    export default {
      name: "File",
      data(){
          return {
            tableData:[],
            name:'',
            multipleSelection:[],
            pageNum:1,
            pageSize:10,
            totalNum:10
          }
        },
      created() {
        this.load()
      },
      methods:{
        load(){
          this.request.get('/file/page',{
            params: {
              pageNum: this.pageNum,
              pageSize : this.pageSize,
              name : this.name,
            }
          }).then(res => {
            this.tableData = res.data.records
            this.totalNum = res.data.total
          })
        },
        handleDelete(id){
          this.request.delete("/file/"+id).then(res => {
            if (res.status === '200'){
              this.$message.success("删除成功")
              this.pageNum = 1
              this.load();
            }else{
              this.$message.error("删除失败")
            }
          })
        },
        reset(){
          this.name = "",
              this.load();
        },
        handleSelectionChange(val) {
          this.multipleSelection = val
        },
        delBath(){
          let ids = this.multipleSelection.map(v => v.id);
          this.$confirm('此操作将永久删除, 是否继续?', '提示', {
            cancelButtonText: '取消',
            confirmButtonText: '确定',
            type: 'warning'
          }).then(() => {
            console.log("shanchu")
            this.request.post("/file/del/batch",ids).then(res => {
              if (res.status === '200'){
                this.$message.success("删除成功")
                this.load();
              }else{
                this.$message.error("删除失败")
              }
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });

        },
        handleSizeChange(pageSize){
          this.pageSize = pageSize
          this.load();
        },
        handleCurrentChange(pageNum){
          this.pageNum = pageNum
          this.load()
        },
        handleFileUploadSuccess(res){
            this.load();
        },
        downLoad(url){
          window.open(url)
        },
        changEnable(row){
            this.request.post('/file/update',row).then(res => {
              if (res.status === '200'){
                this.$message.success("成功")
              }
            })
        },
        preview(url) {
          window.open('https://file.keking.cn/onlinePreview?url=' + encodeURIComponent(window.btoa((url))))
        },
      }
    }
</script>

<style scoped>

</style>